package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamMemberService;
import com.fan.teamer.service.TeamService;

public class RemoveMemberFromTeamProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger("RemoveMemberFromTeamProcess");

	private Member operator;
	private Team team;
	private Member member;
	private TeamMemberService teamMemberService;
	private TeamService teamService;
	private MemberService memberService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		RemoveMemberFromTeamProcess.logger = logger;
	}

	public Member getOperator() {
		return operator;
	}

	public void setOperator(Member operator) {
		this.operator = operator;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public TeamMemberService getTeamMemberService() {
		return teamMemberService;
	}

	public void setTeamMemberService(TeamMemberService teamMemberService) {
		this.teamMemberService = teamMemberService;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public int initCheck() {
		if (teamService == null || memberService == null
				|| teamMemberService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (operator == null || member == null || team == null) {
			setErrorMessage("Domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!memberService.hasMember(operator.getEmail())) {
			setErrorMessage("Member " + operator.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!memberService.hasMember(member.getEmail())) {
			setErrorMessage("Member " + member.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!teamService.hasTeam(team.getId())) {
			setErrorMessage("Team " + team.getName()
					+ " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}

		if (!teamService.hasMember(team, operator)) {
			setErrorMessage("Team " + team.getName()
					+ " doesn't have Member " + operator.getEmail());
			return TeamerGlobalVariables.FAILED;
		}
		if (!teamService.hasMember(team, member)) {
			setErrorMessage("Team " + team.getName()
					+ " doesn't have Member " + member.getEmail());
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

	// remove myself
	public int removeSameMember() {
		// start removing
		try {
			teamService.deleteMemberFromTeam(team, operator);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Member: " + member.getEmail()
					+ " cannot be removed!");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

	public int removeDifferentMember() {
		// remove other member
		// check if member has the authority to remove members
		String operatorRole = memberService.getMemberRoleInTeam(operator, team);

		// participant cannot remove others
		if (operatorRole.equals(TeamerGlobalVariables.PARTICIPANT)) {
			setErrorMessage("Member: " + operator.getEmail()
					+ " doen't have right to remove " + member.getEmail());
			return TeamerGlobalVariables.FAILED;
		} else {
			// member has right to remove another member
			try {
				teamService.deleteMemberFromTeam(team, member);
			} catch (Exception e) {
				e.printStackTrace();
				setErrorMessage("Member: " + member.getEmail()
						+ " cannot be removed!");
				return TeamerGlobalVariables.FAILED;
			}

		}
		return TeamerGlobalVariables.SUCCESS;

	}

	public int checkAutority() {

		// organiser cannot be removed by other members
		if (memberService.getMemberRoleInTeam(member, team).equals(
				TeamerGlobalVariables.ORGANIZER)) {
			setErrorMessage(TeamerGlobalVariables.ORGANIZER
					+ " cannot be deleted from team by member "
					+ operator.getEmail());
			return TeamerGlobalVariables.FAILED;
		} else {
			return TeamerGlobalVariables.SUCCESS;
		}

	}

	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}
		if (checkAutority() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		// quit from team
		if (operator.getEmail().equals(member.getEmail())) {
			if (removeSameMember() == TeamerGlobalVariables.FAILED) {
				return TeamerGlobalVariables.FAILED;
			}
		}
		// remove different member
		else {
			if (removeDifferentMember() == TeamerGlobalVariables.FAILED) {
				return TeamerGlobalVariables.FAILED;
			}
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
